package com.bw.gmall.realtime.app.dwd;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONAware;
import com.alibaba.fastjson.JSONObject;
import com.bw.gmall.realtime.utils.MyKafkaUtil;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.cep.CEP;
import org.apache.flink.cep.PatternFlatSelectFunction;
import org.apache.flink.cep.PatternFlatTimeoutFunction;
import org.apache.flink.cep.PatternStream;
import org.apache.flink.cep.pattern.Pattern;
import org.apache.flink.cep.pattern.conditions.SimpleCondition;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaProducer;
import org.apache.flink.util.Collector;
import org.apache.flink.util.OutputTag;

import java.util.List;
import java.util.Map;

public class DwdTrafficUserJumpDetail {
    public static void main(String[] args) throws Exception {
        //todo 1.初始化上下文
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        //todo 2.设置并行度
        env.setParallelism(1);
        //todo 3.读取 kafka dwd_traffic_page_log 日志数据
        String topic = "dwd_traffic_page_log";
        String groupId = "DwdTrafficUserJumpDetail";

        DataStreamSource<String> KafkaConsumer = env.addSource(MyKafkaUtil.getFlinkKafkaConsumer(topic, groupId));
        //todo 4.转换结构
        SingleOutputStreamOperator<JSONObject> ds1 = KafkaConsumer.flatMap(new FlatMapFunction<String, JSONObject>() {
            @Override
            public void flatMap(String s, Collector<JSONObject> collector) throws Exception {

                try {
                    JSONObject jsonOb = JSON.parseObject(s);

                    collector.collect(jsonOb);
                } catch (Exception e) {
                    System.out.println("脏数据:" + s);
                }

            }
        });

        //todo 5.设置水位线  用于用户跳出统计

        SingleOutputStreamOperator<JSONObject> withWatermarkStream = ds1.assignTimestampsAndWatermarks(WatermarkStrategy.<JSONObject>forMonotonousTimestamps().withTimestampAssigner(new SerializableTimestampAssigner<JSONObject>() {
            @Override
            public long extractTimestamp(JSONObject jsonObject, long l) {
                return jsonObject.getLong("ts");
            }
        }));

        //todo 6.按照mid分组

        KeyedStream<JSONObject, String> keyedStream = withWatermarkStream.keyBy(o -> o.getJSONObject("common").getString("mid"));

        //todo 7.定义cep 匹配规则
        //Pattern.<JSONObject>begin("first") 定义为其实事件,这个名称用于后续从匹配结果中提取事件
        Pattern<JSONObject, JSONObject> pattern = Pattern.<JSONObject>begin("first")
                .where(new SimpleCondition<JSONObject>() {
                    @Override
                    public boolean filter(JSONObject jsonObject) throws Exception {
                        String lastPageId = jsonObject.getJSONObject("page").getString("last_page_id");
                        return lastPageId == null;
                    }
                })
                //next 要求两个事件必须连续发生（中间不能有其他事件）
                .next("second")
                .where(new SimpleCondition<JSONObject>() {
                    @Override
                    public boolean filter(JSONObject jsonObject) throws Exception {
                        String lastPageId = jsonObject.getJSONObject("page").getString("last_page_id");
                        return lastPageId == null;
                    }
                    // within(Time.seconds(10)) 时间窗口限制：两个事件必须在 10 秒内完成匹配
                }).within(Time.seconds(10));


        // TODO 8. 把 Pattern 应用到流上
        PatternStream<JSONObject> patternStream = CEP.pattern(keyedStream, pattern);

        // TODO 9. 提取匹配上的事件以及超时事件

        OutputTag<JSONObject> outputTag = new OutputTag<JSONObject>("timeoutTag"){};

        // flatSelect 方法 同时处理匹配成功的事件和超时事件
        SingleOutputStreamOperator<JSONObject> flatResult = patternStream.flatSelect(
                outputTag,
                //  PatternFlatTimeoutFunction：处理超时事件
                new PatternFlatTimeoutFunction<JSONObject, JSONObject>() {


                    @Override
                    public void timeout(Map<String, List<JSONObject>> map, long l, Collector<JSONObject> collector) throws Exception {
                        JSONObject element = map.get("first").get(0);
                        collector.collect(element);
                    }
                },
                // PatternFlatSelectFunction：处理完全匹配的事件
                new PatternFlatSelectFunction<JSONObject, JSONObject>() {


                    @Override
                    public void flatSelect(Map<String, List<JSONObject>> map, Collector<JSONObject> collector) throws Exception {
                        JSONObject element = map.get("first").get(0);
                        collector.collect(element);
                    }
                }
        );

        flatResult.print("主流");
        DataStream<JSONObject> timeoutFlatResult = flatResult.getSideOutput(outputTag);
        timeoutFlatResult.print("超时流");

        //todo 10.合并两个流并将数据写出到kagka中

        DataStream<JSONObject> union = flatResult.union(timeoutFlatResult);

        union.print(">>>>>>>>>>>>>");

        String writeTopic = "dwd_traffic_user_jump_detail";
        FlinkKafkaProducer<String> flinkKafkaProducer = MyKafkaUtil.getFlinkKafkaProducer(writeTopic);
        union.map(JSONAware::toJSONString).addSink(flinkKafkaProducer);


        env.execute();
    }
}
